package com.ln.lnzw.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.ln.lnzw.R;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.bean.RegistBean;
import com.ln.lnzw.net.HttpMethods;
import com.ln.lnzw.utils.ToastFactory;
import com.ln.lnzw.utils.TokenUtils;
import com.ln.lnzw.view.ClearableEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/5/25.
 * 自然人注册
 */

public class NaturalPersonRegisteredActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_yanzheng)
    ImageView ivYanzheng;
    @BindView(R.id.tv_yanzheng)
    TextView tvYanzheng;
    @BindView(R.id.yanzheng)
    LinearLayout yanzheng;
    @BindView(R.id.iv_yonghu)
    ImageView ivYonghu;
    @BindView(R.id.tv_yonghu)
    TextView tvYonghu;
    @BindView(R.id.ll_yonghu)
    LinearLayout llYonghu;
    @BindView(R.id.iv_set)
    ImageView ivSet;
    @BindView(R.id.tv_set)
    TextView tvSet;
    @BindView(R.id.ll_set)
    LinearLayout llSet;
    @BindView(R.id.rl_tijaio1)
    RelativeLayout rlTijaio1;
    @BindView(R.id.rl_verifyphone)
    RelativeLayout rlVerifyphone;
    @BindView(R.id.rl_tijaio2)
    RelativeLayout rlTijaio2;
    @BindView(R.id.rl_userinfo)
    LinearLayout rlUserinfo;
    @BindView(R.id.rl_registered)
    RelativeLayout rlRegistered;
    @BindView(R.id.ll_setmima)
    LinearLayout llSetmima;
    @BindView(R.id.tv_tongyi)
    TextView tvTongyi;
    @BindView(R.id.rl_tongyi)
    RelativeLayout rlTongyi;
    @BindView(R.id.phone)
    ClearableEditText phone;
    @BindView(R.id.cet_yanzhengma)
    ClearableEditText cetYanzhengma;
    @BindView(R.id.cet_name)
    ClearableEditText cetName;
    @BindView(R.id.cet_truename)
    ClearableEditText cetTruename;
    @BindView(R.id.cet_shenfenzheng)
    ClearableEditText cetShenfenzheng;
    @BindView(R.id.cet_mima_one)
    ClearableEditText cetMimaOne;
    @BindView(R.id.cet_mima_two)
    ClearableEditText cetMimaTwo;
    @BindView(R.id.cet_youxiang)
    ClearableEditText cetYouxiang;
    @BindView(R.id.iv_duihao)
    ImageView ivDuihao;
    private int flag = 0;
    private boolean tag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_natural_person_registered);
        ButterKnife.bind(this);
        setListener();
    }

    private void setListener() {
        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });

        cetYanzhengma.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });
        cetName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetName.getText()) || TextUtils.isEmpty(cetTruename.getText()) || TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    rlTijaio2.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetName.getText()) && !TextUtils.isEmpty(cetTruename.getText()) && !TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    rlTijaio2.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }

            }
        });
        cetTruename.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetName.getText()) || TextUtils.isEmpty(cetTruename.getText()) || TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    rlTijaio2.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetName.getText()) && !TextUtils.isEmpty(cetTruename.getText()) && !TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    rlTijaio2.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetShenfenzheng.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetName.getText()) || TextUtils.isEmpty(cetTruename.getText()) || TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    rlTijaio2.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetName.getText()) && !TextUtils.isEmpty(cetTruename.getText()) && !TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    rlTijaio2.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetMimaOne.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (tag) {//未选中状态
                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                }
            }
        });
        cetMimaTwo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (tag) {//未选中状态
                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                }
            }
        });
        cetYouxiang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (tag) {//未选中状态
                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                }
            }
        });

    }

    private void editTextIsNo() {
        if (TextUtils.isEmpty(phone.getText()) || TextUtils.isEmpty(cetYanzhengma.getText())) {
            rlTijaio1.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
        }
    }

    private void editTextIsYes() {
        if (!TextUtils.isEmpty(phone.getText()) && !TextUtils.isEmpty(cetYanzhengma.getText())) {
            rlTijaio1.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
        }
    }

    @OnClick({R.id.iv_back, R.id.yanzheng, R.id.tv_tongyi,R.id.rl_tongyi, R.id.ll_yonghu, R.id.ll_set, R.id.rl_tijaio1, R.id.rl_tijaio2, R.id.rl_registered, R.id.ll_setmima})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                Log.i("123", "onViewClicked: " + flag);
                if (flag == 0) {
                    finish();
                } else if (flag == 1) {  //1-0
                    flag = 0;
                    rlVerifyphone.setVisibility(View.VISIBLE);
                    rlUserinfo.setVisibility(View.GONE);
                    //恢复
                    ivYonghu.setImageResource(R.mipmap.icon_yhxx_no);
                    tvYonghu.setTextColor(getResources().getColor(R.color.mine_small_color));
                } else { //2-1
                    flag = 1;
                    llSetmima.setVisibility(View.GONE);
                    rlUserinfo.setVisibility(View.VISIBLE);
                    ivSet.setImageResource(R.mipmap.icon_szmm_no);
                    tvSet.setTextColor(getResources().getColor(R.color.mine_small_color));
                }

                break;
            case R.id.rl_tijaio1:
                if (TextUtils.isEmpty(phone.getText()) || TextUtils.isEmpty(cetYanzhengma.getText())) {
                    return;
                }
                if (!TextUtils.isEmpty(phone.getText()) && !TextUtils.isEmpty(cetYanzhengma.getText())) {
                    if (!RegexUtils.isMobileExact(phone.getText())){
                        ToastFactory.getToast(activity, "请输入正确的手机号").show();
                        return;
                    }
                }
                //校验验证码

                flag = 1;
                rlVerifyphone.setVisibility(View.GONE);
                rlUserinfo.setVisibility(View.VISIBLE);
                ivYonghu.setImageResource(R.mipmap.icon_yhxx_yes);
                tvYonghu.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                break;
            case R.id.rl_tijaio2:
                if (TextUtils.isEmpty(cetName.getText()) || TextUtils.isEmpty(cetTruename.getText()) || TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    return;
                }
                flag = 2;
                llSetmima.setVisibility(View.VISIBLE);
                rlUserinfo.setVisibility(View.GONE);
                ivSet.setImageResource(R.mipmap.icon_szmm_yes);
                tvSet.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                break;
            case R.id.rl_registered:
                if (tag) {

                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        //判断俩次密码是否一致
                        if (!cetMimaOne.getText().toString().equals(cetMimaTwo.getText().toString())) {
                            ToastFactory.getToast(this, "密码不一致").show();
                            return;
                        }
                        //判断邮箱格式
                        if (!RegexUtils.isEmail(cetYouxiang.getText())) {
                            ToastFactory.getToast(this, "邮箱格式错误").show();
                            return;
                        }
//                        ActivityUtils.startActivity(activity, VerifiedActivity.class);
                        initData();//注册请求
                    }
                }

                break;
            case R.id.tv_tongyi:
                Bundle b = new Bundle();
                b.putString("webUrl", "file:///android_asset/LNnet.html");
                b.putString("name", "辽宁政府服务APP用户协议");
                ActivityUtils.startActivity(b, activity, UserAgreementActivity.class);
                break;
            case R.id.rl_tongyi:
                if (tag) {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        ivDuihao.setImageResource(R.mipmap.icon_zhuche_tongyi_yes);
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        ivDuihao.setImageResource(R.mipmap.icon_zhuche_tongyi_yes);
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                    tag = false;
                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        ivDuihao.setImageResource(R.mipmap.icon_zhuche_tongyi_no);
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        ivDuihao.setImageResource(R.mipmap.icon_zhuche_tongyi_no);
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                    tag = true;
                }

                break;
        }
    }

    private void initData() {
        HttpMethods.getInstanceCenter().info.naturalPersonRegist(TokenUtils.getInstance().GetGUID(),cetName.getText().toString(),cetMimaOne.getText().toString(),cetTruename.getText().toString(),cetShenfenzheng.getText().toString(),phone.getText().toString(),cetYouxiang.getText().toString(),cetYanzhengma.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RegistBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(RegistBean registBean) {
                        Log.i("555", "onNext: "+registBean.toString());
                        if ("200".equals(registBean.getCode())) {
                            ToastFactory.getToast(activity,registBean.getMsg()).show();
//                            ActivityUtils.startActivity(activity,VerifiedActivity.class);

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("555", "onNext: "+e.toString());
                    }

                    @Override
                    public void onComplete() {
                        finish();
                    }
                });

    }

    @Override
    public void onBackPressedSupport() {
        if (flag == 0) {
            super.onBackPressedSupport();
        } else if (flag == 1) {  //1-0
            flag = 0;
            rlVerifyphone.setVisibility(View.VISIBLE);
            rlUserinfo.setVisibility(View.GONE);
            //恢复
            ivYonghu.setImageResource(R.mipmap.icon_yhxx_no);
            tvYonghu.setTextColor(getResources().getColor(R.color.mine_small_color));
        } else { //2-1
            flag = 1;
            llSetmima.setVisibility(View.GONE);
            rlUserinfo.setVisibility(View.VISIBLE);
            ivSet.setImageResource(R.mipmap.icon_szmm_no);
            tvSet.setTextColor(getResources().getColor(R.color.mine_small_color));
        }

    }
}
